package com.xuecheng.fastdfs.demo;

import com.xuecheng.fastdfs.stub.HelloServiceGrpc;
import com.xuecheng.fastdfs.stub.HelloServiceOuterClass;
import io.grpc.ManagedChannel;
import io.grpc.netty.shaded.io.grpc.netty.NegotiationType;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;

public class MyHelloClient {
    public static void main(String[] args) {
        // 指定服务的ip和端口号
        String host = "192.168.0.105";
        int port = 8899;
        // 创建客户端
        ManagedChannel channel = NettyChannelBuilder.forAddress(host, port)
                .negotiationType(NegotiationType.PLAINTEXT)
                .build();
        HelloServiceGrpc.HelloServiceBlockingStub blockingStub = HelloServiceGrpc.newBlockingStub(channel);
        // 构造请求
        HelloServiceOuterClass.HelloRequest request = HelloServiceOuterClass.HelloRequest.newBuilder()
                .setRequestId(1)
                .setName("zy")
                .build();
        // 请求服务
        HelloServiceOuterClass.HelloResponse response = blockingStub.sayHello(request);
        // 解析结果
        System.out.println("Greeting: " + response.getGreeting());
    }
}
